//
//  AfraDataBase2016010902Movie.m
//  UI_元旦豆瓣
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 31付冬梅. All rights reserved.
//

#import "AfraDataBase2016010902Movie.h"
#import "MovieModel.h"
@implementation AfraDataBase2016010902Movie

+ (AfraDataBase2016010902Movie *)sharedDataBaseMovieHandle {
    static AfraDataBase2016010902Movie *movieBaseHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        movieBaseHandle = [[AfraDataBase2016010902Movie alloc] init];
    });
    return movieBaseHandle;
}

- (void)openMovieDB {
    //获取到沙盒路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //拼接数据库路径
    NSString *dbpath = [document stringByAppendingPathComponent:@"AfraDataBase2016010902Movie"];
    NSLog(@"沙盒：%@",dbpath);
    //打开数据库
    int result = sqlite3_open(dbpath.UTF8String, &movieDataBase);
    if (result == SQLITE_OK) {
        NSLog(@"打开电影数据库");
    } else {
        NSLog(@"打开电影数据库失败");
    }
    
    
}

- (void)creatMovieTable {
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, title text, image text)"];
    int result = sqlite3_exec(movieDataBase, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建电影数据库表成功");
    } else {
        NSLog(@"创建电影数据库表失败");
    }
}

- (void)insertMovieModel:(MovieModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Movie(title, image) values('%@', '%@')", model.title, model.image];
    int result = sqlite3_exec(movieDataBase, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入电影成功");
    } else {
        NSLog(@"插入电影失败");
    }
}

- (void)deleteAllMovieModel {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Movie"];
    int result = sqlite3_exec(movieDataBase, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"清除电影收藏成功");
    } else {
        NSLog(@"清除电影收藏失败");
    }
}
- (void)deleteMovieModel:(MovieModel *)model {
     NSString *sqlStr = [NSString stringWithFormat:@"delete from Movie where title = '%@'", model.title];
    int result = sqlite3_exec(movieDataBase, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除电影成功");
        
    } else {
        NSLog(@"删除电影失败");
    }
}

- (NSMutableArray *)selectMovieTable {
    NSString *sqlStr = [NSString stringWithFormat:@"select title from Movie"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(movieDataBase, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arrModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *image = sqlite3_column_text(stmt, 2);
            MovieModel *model = [MovieModel movieModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            model.image = [NSString stringWithUTF8String:(const char *)image];
            [arrModel addObject:model];
        }
    } else {
        NSLog(@"查询电影收藏失败");
    }
    return arrModel;
}


@end
